package com.app.piemoment;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.app.piemoment.api.ApiResult;
import com.app.piemoment.api.ApiReturnResultListener;
import com.app.piemoment.api.UserAPI;
import com.app.piemoment.config.ApiConfig;
import com.app.piemoment.entity.LoginInfo;
import com.app.piemoment.entity.PieUser;
import com.app.piemoment.log.PMLog;
import com.app.piemoment.net.HttpResultJson;
import com.app.piemoment.net.NetService;

public class LoginUserActivity extends Activity {
	private EditText ETEmailUser, ETpassword;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.username);

		Button _login = (Button) findViewById(R.id._login);

		ETEmailUser = (EditText) findViewById(R.id.ETEmailUser);
		ETpassword = (EditText) findViewById(R.id.ETpassword);

		_login.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (ETEmailUser.getText().toString().equals("")) {
					Toast.makeText(LoginUserActivity.this, "请输入用户邮箱或用户名或手机号 ",
							Toast.LENGTH_LONG).show();
					return;
				}

				if (ETpassword.getText().toString().equals("")) {
					Toast.makeText(LoginUserActivity.this, "请输入密码 ",
							Toast.LENGTH_LONG).show();
					return;
				}

				LoginAsy loginAsy = new LoginAsy();
				loginAsy.execute();
			}
		});

		Button _tiyan = (Button) findViewById(R.id._tiyan);
		_tiyan.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Log.e("onClick", "click test");
				Intent i = new Intent();
				// i.setClass(LoginActivity.this, TalkActivity.class);
				i.setClass(LoginUserActivity.this,
						ComplateUserInfo5Activity.class);

				startActivity(i);
			}
		});
		Button _regedit = (Button) findViewById(R.id._regedit);
		_regedit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent();
				i.setClass(LoginUserActivity.this, RegeditActivity.class);
				startActivity(i);
			}
		});

	}

	public void startPair() {
		Intent i = new Intent();
		i.setClass(LoginUserActivity.this, MatchWaitActivity.class);
		startActivity(i);
	}

	ProgressDialog bDialog;

	class LoginAsy extends AsyncTask<Object, Object, LoginInfo> {

		@Override
		protected LoginInfo doInBackground(Object... param) {

			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("_format", "json"));
			params.add(new BasicNameValuePair(ApiConfig.USERKEY, ETEmailUser
					.getText().toString()));
			params.add(new BasicNameValuePair(ApiConfig.DEFAULT_TOKENS,
					ApiConfig.DEFAULT_TOKEN_VALUE));
			params.add(new BasicNameValuePair("passwd", ETpassword.getText()
					.toString()));
			HttpResultJson result = NetService.httpPostReturnJson(
					LoginUserActivity.this, ApiConfig.URL_ROOT
							+ ApiConfig.LOGIN_URL, params);
			LoginInfo.getInstance().parse(result.getJson());

			return null;
		}

		@Override
		protected void onCancelled() {
			bDialog.dismiss();
			super.onCancelled();
		}

		@Override
		protected void onPostExecute(LoginInfo result) {

			if (LoginInfo.getInstance().isError) {
				PMLog.e("errorMsg", "erroMsg is "
						+ LoginInfo.getInstance().getErrorMsg());

				Toast.makeText(LoginUserActivity.this,
						LoginInfo.getInstance().getErrorMsg(),
						Toast.LENGTH_LONG).show();
			} else {
				if (LoginInfo.getInstance().getAccess_token() != null) {
					Toast.makeText(LoginUserActivity.this, "登录成功",
							Toast.LENGTH_SHORT).show();

					getPersonInfo();

					LoginUserActivity.this.finish();
				}
			}
			super.onPostExecute(result);
		}

		@Override
		protected void onPreExecute() {
			bDialog = ProgressDialog.show(LoginUserActivity.this, "请稍候",
					"登陆中....");
			bDialog.show();
			super.onPreExecute();
		}

		@Override
		protected void onProgressUpdate(Object... values) {
			super.onProgressUpdate(values);
		}

	}

	protected static final int LOAD_PROGRESS = 1111;
	JSONObject jsonEntity;

	private void getPersonInfo() {
		UserAPI api = new UserAPI(this);

		api.setReturnResultListener(new ApiReturnResultListener() {

			@Override
			public <T> void onReturnSucceedResult(int requestCode,
					ApiResult<T> apiResult) {

				if (ApiResult.RESULT_OK == apiResult.getResultCode()) {
					jsonEntity = apiResult.getJsonEntity();
					try {
						if (jsonEntity != null) {
							if (jsonEntity.has("status")) {
								String result = jsonEntity.getString("status");
								if (ApiResult.Success.equals(result)) {
									if (jsonEntity.has("data")) {
										JSONArray array = jsonEntity
												.getJSONArray("data");
										PieUser pieUser = new PieUser();

									}
								}
							}
						}
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}
				startPair();

			}

			@Override
			public <T> void onReturnLoading(int requestCode) {

			}

			@Override
			public <T> void onReturnFailResult(int requestCode,
					ApiResult<T> apiResult) {

				bDialog.dismiss();
				try {
					Toast.makeText(
							LoginUserActivity.this,
							apiResult.getJsonEntity().getJSONArray("message")
									.getJSONObject(0).getString("message"),
							Toast.LENGTH_LONG).show();
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		});
		api.getMyInfo(1, LoginUserActivity.this, LoginInfo.getInstance()
				.getPuid()
				+ "");
	}

}
